package elevador;

import java.util.List;

/**
 * 
 * @author Victor
 * @author Breno
 */
public class Sensor extends Thread {

	private Elevator elevator;

	public Sensor(Elevator elevator) {
		super("Sensor-" + elevator.getNumber());
		this.elevator = elevator;
	}

	@Override
	public void run() {
		try {
			while (true) {
				if (elevator.isSensorActived()) {
					sleep(3000);
					if (!elevator.isSensorReseted()) {
						synchronized (elevator) {
							elevator.setSensorActived(false);
							
							MainGUI mainGUI = Scheduler.getInstance().getMainGUI();
							Floor f = mainGUI.getFloor(elevator.getCurrentFloor());
							List<Integer> destinyFloors = f.getDestinys(elevator.getNumber());
							for (Integer floor : destinyFloors) {
								elevator.addRoute(floor);
							}
							destinyFloors.clear();
							
							elevator.setDoorClosed();
							elevator.updateFloors();
						}
					} else {
						elevator.setSensorReseted(false);
					}
				}
				sleep(50);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
